package org.easyexcel;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import org.apache.poi.ss.usermodel.*;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @ClassName MyMerge
 * @Description
 * @Author c.w
 * @Date 2025/7/28 16:59
 * @Version 1.0.0
 */
public class ConsumeRecordWriteHandler implements CellWriteHandler {

	private Set<Integer> rightCol;

	//加粗样式
	private CellStyle rightNumberStyle;
	private Font boldFont;

	//不加粗样式
	private CellStyle rightNumberStyleNoBold;
	private Font noBoldFont;

	public ConsumeRecordWriteHandler(HashSet<Integer> rightCol) {
		this.rightCol = rightCol;
	}

	@Override
	public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {

	}

	@Override
	public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer integer, Boolean aBoolean) {

	}

	@Override
	public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer integer, Boolean aBoolean) {

	}

	@Override
	public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
		int rowIndex = cell.getRowIndex();
		int columnIndex = cell.getColumnIndex();

		Workbook workbook = cell.getSheet().getWorkbook();
		Sheet sheet = cell.getSheet();
		if (rightNumberStyle == null) {
			rightNumberStyle = workbook.createCellStyle();
			if (boldFont == null) {
				boldFont = workbook.createFont();
				boldFont.setFontName("微软雅黑");
				boldFont.setBold(true);
				boldFont.setFontHeightInPoints((short) 10);
			}
			rightNumberStyle.setFont(boldFont);
			rightNumberStyle.setAlignment(HorizontalAlignment.RIGHT);
			rightNumberStyle.setVerticalAlignment(VerticalAlignment.CENTER);
			DataFormat numberFormat = workbook.createDataFormat();
			rightNumberStyle.setDataFormat(numberFormat.getFormat("0.00"));
			setBorder4(rightNumberStyle, BorderStyle.THIN);
		}
		if (rightNumberStyleNoBold == null) {
			rightNumberStyleNoBold = workbook.createCellStyle();
			if (noBoldFont == null) {
				noBoldFont = workbook.createFont();
				noBoldFont.setFontName("微软雅黑");
				noBoldFont.setFontHeightInPoints((short) 10);
			}
			rightNumberStyleNoBold.setFont(noBoldFont);
			rightNumberStyleNoBold.setAlignment(HorizontalAlignment.RIGHT);
			rightNumberStyleNoBold.setVerticalAlignment(VerticalAlignment.CENTER);
			DataFormat numberFormat = workbook.createDataFormat();
			rightNumberStyleNoBold.setDataFormat(numberFormat.getFormat("0.00"));
			setBorder4(rightNumberStyleNoBold, BorderStyle.THIN);
		}
		if (rowIndex >= 1 && rightCol.contains(columnIndex)) {
			Cell cellIndexOf1 = sheet.getRow(rowIndex).getCell(1);
			if (cellIndexOf1 != null && (cellIndexOf1.getStringCellValue().contains("小计") || cellIndexOf1.getStringCellValue().contains("总计"))) {
				cell.setCellStyle(rightNumberStyle);
			} else {
				cell.setCellStyle(rightNumberStyleNoBold);
			}
		}
	}

	private void setAlignmentCenter(CellStyle style) {
		style.setAlignment(HorizontalAlignment.CENTER);
		style.setVerticalAlignment(VerticalAlignment.CENTER);
	}

	private void setBorder4(CellStyle cellStyle, BorderStyle borderStyle) {
		cellStyle.setBorderBottom(borderStyle);
		cellStyle.setBorderLeft(borderStyle);
		cellStyle.setBorderRight(borderStyle);
		cellStyle.setBorderTop(borderStyle);
	}

	//判断单个单元格是否为空
	public boolean isEmptyCell(Cell cell) {
		if (cell == null || cell.getCellTypeEnum() == CellType.BLANK) {
			return true;
		}
		return false;
	}

}